#include <boost/asio.hpp>
#include <iostream>
#include <string>

using boost::asio::ip::udp;

int main() {
    try {
        boost::asio::io_context io;
        
        // 创建UDP socket
        udp::socket socket(io, udp::endpoint(udp::v4(), 0));
        
        // 发送数据
        udp::resolver resolver(io);
        udp::resolver::results_type endpoints = 
            resolver.resolve(udp::v4(), "127.0.0.1", "8080");
        
        std::string message = "Hello, UDP!";
        socket.send_to(boost::asio::buffer(message), *endpoints.begin());
        
        // 接收数据
        char recv_buf[128];
        udp::endpoint sender_endpoint;
        size_t len = socket.receive_from(
            boost::asio::buffer(recv_buf), sender_endpoint);
        
        std::cout << "收到 " << len << " 字节: " 
                  << std::string(recv_buf, len) << std::endl;
    }
    catch (std::exception& e) {
        std::cerr << "错误: " << e.what() << std::endl;
    }
    
    return 0;
}